package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.Camera;
import dao.CameraDao;
import dao.DaoFactory;

public class CameraList extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	final CameraDao cameraDao = DaoFactory.getInstance().getCameraDao();

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		Long userId = Long.parseLong(req.getParameter("userId"));
		if(userId == 0) {
			resp.sendRedirect("userlist");
			return;
		}
		List<Camera> cameras = cameraDao.list(userId);
		req.setAttribute("cameras", cameras);
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/cameralist.jsp");
		rd.forward(req, resp);
		
	}
	
}
